cssimagefallback: Use gtk_css_parser_consume_function()
authorBenjamin Otte <otte@redhat.com>
Sat, 30 Mar 2019 23:52:22 +0000 (00:52 +0100)
committerBenjamin Otte <otte@redhat.com>
Fri, 12 Apr 2019 17:34:28 +0000 (19:34 +0200)
gtk/gtkcssimagefallback.c

index f109337468f7bbacd6d26ce9efb02ac158a1e8be..c33912e00f8ce7b6c69cd84365233ab6fac1090a 100644 (file)
@@ -175,57 +175,68 @@ gtk_css_image_fallback_compute (GtkCssImage      *image,
     return GTK_CSS_IMAGE (g_object_ref (fallback));
 }
 
-static gboolean
-gtk_css_image_fallback_parse (GtkCssImage  *image,
-                              GtkCssParser *parser)
+typedef struct
 {
-  GtkCssImageFallback *fallback = GTK_CSS_IMAGE_FALLBACK (image);
+  GtkCssValue *color;
   GPtrArray *images;
-  GtkCssImage *child;
+} ParseData;
+
+static guint
+gtk_css_image_fallback_parse_arg (GtkCssParser *parser,
+                                  guint         arg,
+                                  gpointer      _data)
+{
+  ParseData *data = _data;
 
-  if (!_gtk_css_parser_try (parser, "image", TRUE))
+  if (data->color != NULL)
     {
-      _gtk_css_parser_error (parser, "'image'");
-      return FALSE;
+      _gtk_css_parser_error (parser, "The color must be the last parameter");
+      return 0;
     }
+  else if (_gtk_css_image_can_parse (parser))
+    {
+      GtkCssImage *image = _gtk_css_image_new_parse (parser);
+      if (image == NULL)
+        return 0;
 
-  if (!_gtk_css_parser_try (parser, "(", TRUE))
+      g_ptr_array_add (data->images, image);
+      return 1;
+    }
+  else
     {
-      _gtk_css_parser_error (parser,
-                             "Expected '(' after 'image'");
-      return FALSE;
+      data->color = _gtk_css_color_value_parse (parser);
+      if (data->color == NULL)
+        return 0;
+
+      return 1;
     }
+}
 
-  images = g_ptr_array_new_with_free_func (g_object_unref);
+static gboolean
+gtk_css_image_fallback_parse (GtkCssImage  *image,
+                              GtkCssParser *parser)
+{
+  GtkCssImageFallback *self = GTK_CSS_IMAGE_FALLBACK (image);
+  ParseData data = { NULL, NULL };
 
-  do
+  if (!gtk_css_parser_has_function (parser, "image"))
     {
-      child = NULL;
-      if (_gtk_css_image_can_parse (parser))
-        child = _gtk_css_image_new_parse (parser);
-      if (child == NULL)
-        {
-          fallback->color = _gtk_css_color_value_parse (parser);
-          if (fallback->color)
-            break;
-
-          g_ptr_array_free (images, TRUE);
-          return FALSE;
-        }
-      g_ptr_array_add (images, child);
+      _gtk_css_parser_error (parser, "Expected 'image('");
+      return FALSE;
     }
-  while ( _gtk_css_parser_try (parser, ",", TRUE));
 
-  if (!_gtk_css_parser_try (parser, ")", TRUE))
+  data.images = g_ptr_array_new_with_free_func (g_object_unref);
+
+  if (!gtk_css_parser_consume_function (parser, 1, G_MAXUINT, gtk_css_image_fallback_parse_arg, &data))
     {
-      g_ptr_array_free (images, TRUE);
-      _gtk_css_parser_error (parser,
-                             "Expected ')' at end of 'image'");
+      g_clear_pointer (&data.color, _gtk_css_value_unref);
+      g_ptr_array_free (data.images, TRUE);
       return FALSE;
     }
 
-  fallback->n_images = images->len;
-  fallback->images = (GtkCssImage **) g_ptr_array_free (images, FALSE);
+  self->color = data.color;
+  self->n_images = data.images->len;
+  self->images = (GtkCssImage **) g_ptr_array_free (data.images, FALSE);
 
   return TRUE;
 }